//
//  MBStoryHotSearchCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/27.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBStoryHotSearchCell.h"

@implementation MBStoryHotSearchCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
}

- (void)layoutData:(NSArray *)dataArr
{
    [self.contentView removeAllSubviews];
    
    UIFont *font = [UIFont systemFontOfSize:13.f];
    float allWidth = (ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):35*PERCEN);
    float allHeight = 20*PERCEN;
    int index = 0; //保存换行次数
    for (int i=0; i<dataArr.count; i++) {
        NSString *title = [dataArr objectAtIndex:i];
        float itemWidth = [title boundingRectWithSize:CGSizeMake(1000, 70*PERCEN) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:NULL].size.width+(WIDTH_5S ? 20 :30);
        //frame
        if (allWidth+itemWidth+(ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):35*PERCEN) > SCREEN_WIDTH) {
            //换行
            allWidth = (ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):35*PERCEN);
            
            ++index;
            allHeight += (90*PERCEN*index);
        }
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(allWidth, allHeight, itemWidth, 70*PERCEN);
        [bt setTitle:title forState:UIControlStateNormal];
        [bt setTitle:title forState:UIControlStateHighlighted];
        [bt setTitleColor:UIColorFromHex(0x999999) forState:UIControlStateNormal];
        [bt setTitleColor:UIColorFromHex(0x999999) forState:UIControlStateHighlighted];
        bt.titleLabel.font = font;
        bt.layer.cornerRadius = 5.f;
        bt.layer.masksToBounds = YES;
        bt.layer.borderWidth =1.f;
        bt.layer.borderColor = [UIColorFromHex(0x999999)colorWithAlphaComponent:.35].CGColor;
        bt.tag = 9000+i;
        [bt addTarget:self action:@selector(onSelectItemForSearchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:bt];

        //加上间距
        allWidth += itemWidth+30*PERCEN;
    }
}


- (void)onSelectItemForSearchAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForHotSearch:withSelectIndex:)]) {
        [_delegate onSelectItemForHotSearch:self withSelectIndex:((int)bt.tag-9000)];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
